package com.example.wxback.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.wxback.model.BindPhoneRequest;
import com.example.wxback.model.UserInfo;
import com.example.wxback.service.WxUserService;
import com.example.wxback.util.CommonUtil;
import com.example.wxback.util.StringTools;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/wx/user")
public class WxUserController {

    @Autowired
    WxUserService wxUserService;

    @PostMapping("/register")
    public JSONObject register(@RequestBody UserInfo userInfo) {
        if (!StringTools.checkPhone(userInfo.getPhone())) {
            return CommonUtil.errorJson("手机号不合法");
        }
        String checkPassword = StringTools.checkPassword(userInfo.getPassword());
        if (!StringTools.isNullOrEmpty(checkPassword)) {
            return CommonUtil.errorJson(checkPassword);
        }
        UserInfo userInfoTemp = wxUserService.getUserByPhone(userInfo.getPhone());
        if (userInfoTemp != null) {
            return CommonUtil.errorJson("用户已存在，请勿重复注册");
        }
        userInfo.setMemberType(2);
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        if (wxUserService.insertUser(userInfo)) {
            return CommonUtil.successJson("注册成功");
        } else {
            return CommonUtil.errorJson("注册失败");
        }
    }

    @PutMapping("/bind")
    public JSONObject bindChildPhone(@RequestBody BindPhoneRequest bindPhoneRequest) {
        if (!StringTools.checkPhone(bindPhoneRequest.getPhone())) {
            return CommonUtil.errorJson("手机号不合法");
        }
        if (!StringTools.checkPhone(bindPhoneRequest.getPhone())) {
            return CommonUtil.errorJson("被绑定人手机号不合法");
        }
        return null;
    }

    @GetMapping("/users")
    public JSONObject getUser(@Param("pageNum") int pageNum, @Param("pageRow") int pageRow) {
        int total = wxUserService.countUser();
        pageNum = pageNum - 1;
        return CommonUtil.successPage(pageRow, wxUserService.getUserList(pageNum, pageRow), total);
    }

    @GetMapping("/{id}")
    public JSONObject getUser(@PathVariable Integer id) {
        return CommonUtil.successJson(wxUserService.getUser(id));
    }

    @PostMapping("/insert")
    public JSONObject insertUser(@RequestBody UserInfo userInfo) {
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        if (wxUserService.insertUser(userInfo)) {
            return CommonUtil.successJson("添加成功");
        } else {
            return CommonUtil.errorJson("用户添加失败");
        }
    }

    @PutMapping("/update")
    public JSONObject updateUser(@RequestBody UserInfo userInfo) {
        if (StringTools.isNullOrEmpty(userInfo.getId())) {
            return CommonUtil.errorJson("用户id不能为空");
        }
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        if (wxUserService.updateUser(userInfo)) {
            return CommonUtil.successJson("修改成功");
        } else {
            return CommonUtil.errorJson("用户添加失败");
        }
    }

    @PutMapping("/{id}/enable/{type}")
    public JSONObject enableUser(@PathVariable Integer id, @PathVariable Integer type) {
        UserInfo userInfo = wxUserService.getUser(id);
        if (ObjectUtils.isEmpty(userInfo)) {
            return CommonUtil.errorJson("用户不存在");
        }
        if (type > 0) {
            type = 1;
        } else {
            type = 0;
        }
        userInfo.setEnable(type);
        userInfo.setUpdateTime(new Date());
        if (wxUserService.enableUser(userInfo)) {
            return CommonUtil.successJson("状态修改成功");
        } else {
            return CommonUtil.errorJson("状态修改失败");
        }
    }

    @DeleteMapping("/{id}/delete")
    public JSONObject deleteUser(@PathVariable Integer id) {
        UserInfo userInfo = wxUserService.getUser(id);
        if (ObjectUtils.isEmpty(userInfo)) {
            return CommonUtil.errorJson("用户不存在");
        }
        if (userInfo.getMemberType() == 1){
            return CommonUtil.errorJson("当前用户是管理员，禁止删除哦");
        }
        if (wxUserService.deleteUser(id)) {
            return CommonUtil.successJson("删除成功");
        } else {
            return CommonUtil.errorJson("删除失败");
        }
    }
}
